<template>
  <div>
     <van-cell title="搜索历史" value="">
       <div v-if="isDelhistory">
         <!-- <van-button class="delAll" @click="delAllHistory" color="#f00" plain size="mini" >全部删除</van-button> -->
         <van-button class="delAll" @click="$emit('clear-history')" color="#f00" plain size="mini" >全部删除</van-button>
        <van-button class="delAll" @click="isDelhistory = false" color="#3079ff" plain size="mini" >完成</van-button>
       </div>
       <van-icon v-else name="delete" @click="isDelhistory = true"/>
     </van-cell>
     <!-- 历史item -->
     <div>
        <van-cell :title="item" v-for="(item, index) in historyList" :key="index" @click="OnDelHistoryItem(index, item)">
          <van-icon v-if="isDelhistory" name="close"/>
        </van-cell>
     </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      isDelhistory: false
    }
  },
  props: {
    historyList: {
      type: Array,
      required: true
    }
  },
  methods: {
    OnDelHistoryItem (index, item) {
      if (this.isDelhistory) {
        this.historyList.splice(index, 1)
      } else {
        this.$emit('search', item)
      }
    },
    // 全部删除
    delAllHistory () {
      this.historyList.splice(0)
      this.isDelhistory = false
    }
  }
}
</script>

<style scoped lang="less">
.delAll {
  margin-right: 20px;
  color: #f00;
}
</style>
